
import UIKit

class LeftViewController: UIViewController,myProtocol {
    
    // 定义属性
    var mainTab: MainTabBarController?
    var cat:CatViewController?
    var comment:CommentViewController?
    var fest:FestivalViewController?
    var trailer:TrailerViewController?
    var box: BoxOfficeViewController?
    
    
    /**
    5个按钮
    */
    
    
    var view1 : CustomViewButton?
    var view2 : CustomViewButton?
    var view3 : CustomViewButton?
    var view4 : CustomViewButton?
    var view5 : CustomViewButton?
    
    
    lazy var anim: UIDynamicAnimator = {
        return UIDynamicAnimator(referenceView: self.view)
        }()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        var image = UIImage(named:"Milky Way.jpg")
        self.view.layer.contents = image?.CGImage
        self.view.contentMode = UIViewContentMode.ScaleAspectFill
        createViewButton()
        addGest()
        self.mainTab = MainTabBarController()
        self.comment = CommentViewController()
        self.cat = CatViewController()
        self.trailer = TrailerViewController()
        self.box = BoxOfficeViewController()
        
        //        createTableView()
        self.view .addSubview(self.mainTab!.view)
        self.fest = self.mainTab?.firstVC as? FestivalViewController
        self.fest!.mydelegate = self
        var i =  self.mainTab?.firstVC.view.subviews.count
        // Do any additional setup after loading the view.
    }
    
    
    func show(){
        var height:CGFloat = UIScreen.mainScreen().bounds.height
        var width:CGFloat = UIScreen.mainScreen().bounds.width
        var eachHeight = (height - 180)/5
        var  eachX = (0.7 * width - eachHeight)/2
        self.anim .removeAllBehaviors()
        var eachY = 50+0.5*eachHeight + eachHeight + 20
        /**
        *  每一个Y
        */
        var y2 = eachY
        
        var y3 = eachY + eachHeight + 20
        
        var y4 = eachY + eachHeight + 20 + eachHeight + 20
        
        var y5 = eachY + eachHeight + 20 + eachHeight + 20 + 20 + eachHeight
        
        
        var snapp1 =  UISnapBehavior(item: self.view1!, snapToPoint: CGPointMake(eachX, 50+0.5*eachHeight))
        snapp1.damping = 0.5
        var snapp2 =  UISnapBehavior(item: self.view2!, snapToPoint: CGPointMake(eachX, y2))
        snapp2.damping = 0.5
        
        var snapp3 = UISnapBehavior(item: self.view3!, snapToPoint: CGPointMake(eachX, y3))
        snapp3.damping = 0.5
        
        var snapp4 =  UISnapBehavior(item: self.view4!, snapToPoint: CGPointMake(eachX, y4))
        snapp4.damping = 0.5
        
        var snapp5 = UISnapBehavior(item: self.view5!, snapToPoint: CGPointMake(eachX, y5))
        snapp5.damping = 0.5
        
        
        self.anim .addBehavior(snapp1)
        self.anim .addBehavior(snapp2)
        self.anim .addBehavior(snapp3)
        self.anim .addBehavior(snapp4)
        self.anim .addBehavior(snapp5)
        
    }
    
    
  
    // 创建tabelView
   
    func dismiss(){
        
        var height:CGFloat = UIScreen.mainScreen().bounds.height
        var width:CGFloat = UIScreen.mainScreen().bounds.width
        var eachHeight = (height - 180)/5
        var  eachX = (0.7 * width - eachHeight)/2
        self.anim .removeAllBehaviors()
        var eachY = 50 + eachHeight + 20
        /**
        *  每一个Y
        */
        var y2 = eachY
        
        var y3 = eachY + eachHeight + 20
        
        var y4 = self.view3!.frame.origin.y + 20 + eachHeight
        
        var y5 = self.view4!.frame.origin.y + 20 + eachHeight
        
        
        var snapp1 =  UISnapBehavior(item: self.view1!, snapToPoint: CGPointMake(-50, 50))
        var snapp2 =  UISnapBehavior(item: self.view2!, snapToPoint: CGPointMake(-50, y2))
        var snapp3 = UISnapBehavior(item: self.view3!, snapToPoint: CGPointMake(-50, y3))
        var snapp4 =  UISnapBehavior(item: self.view4!, snapToPoint: CGPointMake(-50, y4))
        var snapp5 = UISnapBehavior(item: self.view5!, snapToPoint: CGPointMake(-50, y5))
        
        snapp1.damping = 0.5
        snapp2.damping = 0.5
        snapp3.damping = 0.5
        snapp4.damping = 0.5
        snapp5.damping = 0.5
        
        self.anim .addBehavior(snapp1)
        self.anim .addBehavior(snapp2)
        self.anim .addBehavior(snapp3)
        self.anim .addBehavior(snapp4)
        self.anim .addBehavior(snapp5)
    }
    
    
    
    func createViewButton(){
        var height:CGFloat = UIScreen.mainScreen().bounds.height
        var eachHeight = (height - 180)/5
        var eachY = 50 + eachHeight + 20
        
        
        self.view1 = CustomViewButton(frame: CGRectMake(-100, 50, eachHeight, eachHeight), image: UIImage(named: "iconfont-tianhezuo.png"), title:"美好时光")
        self.view .addSubview(self.view1!)
        
        
        
        self.view2 = CustomViewButton(frame: CGRectMake(-100, eachY, eachHeight, eachHeight), image: UIImage(named: "iconfont-muyangzuo.png"), title:"第二天堂")
        self.view .addSubview(self.view2!)
        
        
        
        self.view3 = CustomViewButton(frame: CGRectMake(-100, eachY+eachHeight + 20, eachHeight, eachHeight), image: UIImage(named: "iconfont-dianyingpiao.png"), title:"最新预告")
        self.view .addSubview(self.view3!)
        
        
        
        
        self.view4 = CustomViewButton(frame: CGRectMake(-100, self.view3!.frame.origin.y + 20 + eachHeight, eachHeight, eachHeight), image: UIImage(named: "iconfont-yugao.png"), title:"精彩影评")
        self.view .addSubview(self.view4!)
        
        
        
        
        self.view5 = CustomViewButton(frame: CGRectMake(-100, self.view4!.frame.origin.y + 20 + eachHeight, eachHeight, eachHeight), image: UIImage(named: "iconfont-jiaofei.png"), title:"实时票房")
        self.view .addSubview(self.view5!)
        
        
        
    }
    
    // 添加手势
    func addGest(){
        var tap1  = UITapGestureRecognizer(target: self, action:("switchPage:"))
        self.view1! .addGestureRecognizer(tap1)
        self.view1?.tag = 100
        
        var tap2  = UITapGestureRecognizer(target: self, action:("switchPage:"))
        self.view2! .addGestureRecognizer(tap2)
        self.view2?.tag = 101
        
        
        
        var tap3  = UITapGestureRecognizer(target: self, action:("switchPage:"))
        self.view3! .addGestureRecognizer(tap3)
        self.view3?.tag = 102
        
        
        
        var tap4  = UITapGestureRecognizer(target: self, action:("switchPage:"))
        self.view4! .addGestureRecognizer(tap4)
        self.view4?.tag = 103
        
        
        
        var tap5  = UITapGestureRecognizer(target: self, action:("switchPage:"))
        self.view5! .addGestureRecognizer(tap5)
        self.view5?.tag = 104
        
        
        
    }
    
    
    
    
    
    
    
    // 选中cell
    func switchPage(gest: UIGestureRecognizer) {
        
        var i = self.mainTab?.firstVC.view.subviews.count
       var view = self.mainTab?.firstVC.view.subviews.last as! UIView
        //        var view = self.mainTab?.firstVC.view.subviews.last as! UIView
        
        switch gest.view!.tag{
        case 100:
            if i == 2{
                println(self.mainTab?.firstVC.view.subviews);
                view .removeFromSuperview()
                self.mainTab?.firstVC.navigationItem.title = "美好时光"
                
            }
            
        case 101:
            if i == 1{
                self.mainTab?.firstVC.view .addSubview(self.cat!.view)
                self.mainTab?.firstVC.navigationItem.title = "第二天堂"
            }else if i == 2 && view != self.cat!.view{
                view .removeFromSuperview()
                self.mainTab?.firstVC.view .addSubview(self.cat!.view)
                self.mainTab?.firstVC.navigationItem.title = "第二天堂"
            }
            
        case 102:
            if i == 1{
                self.mainTab?.firstVC.view .addSubview(self.trailer!.view)
                self.mainTab?.firstVC.navigationItem.title = "最新预告"
                
            }else if i == 2 && view != self.trailer!.view{
                view .removeFromSuperview()
                self.mainTab?.firstVC.view .addSubview(self.trailer!.view)
                self.mainTab?.firstVC.navigationItem.title = "最新预告"
                
            }
            
        case 103:
            if i == 1{
                self.mainTab?.firstVC.view .addSubview(self.comment!.view)
                self.mainTab?.firstVC.navigationItem.title = "精彩影评"
                
            }else if i == 2 && view != self.comment!.view{
                view .removeFromSuperview()
                self.mainTab?.firstVC.view .addSubview(self.comment!.view)
                self.mainTab?.firstVC.navigationItem.title = "精彩影评"
                
            }
        case 104:
            if i == 1{
                self.mainTab?.firstVC.view .addSubview(self.box!.view)
                self.mainTab?.firstVC.navigationItem.title = "实时票房"
                
            }else if i == 2 && view != self.box!.view{
                view .removeFromSuperview()
                self.mainTab?.firstVC.view .addSubview(self.box!.view)
                self.mainTab?.firstVC.navigationItem.title = "实时票房"
                
            }
            
        default:
            back()
        }
        
        back()
    }
    
    
    // 移回来
    func back()
    {
        dismiss()
        
        var height = UIScreen.mainScreen().bounds.height
        
        UIView .animateWithDuration(0.3, animations: { () -> Void in
            self.mainTab?.view.frame.origin.x = 0
            self.mainTab?.view.frame.origin.y = 0
            self.mainTab?.view.frame.size.height = height
            self.fest?.removeClearView()
        })
        
    }
    
    
}
